/********************************************
 * Author: Nikhil Kishore
 * Date: July 26, 2014
 * Description: Random tester that plays full
 * games of dominion.
********************************************/





#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>


int playGame(int seed);
void makeRandomKingdomCards(int seed, int k[]);



int main (int argc, char** argv) {
  	

	int i;

	for( i = 1 ; i <= 1000 ; i++){
	   playGame(i);
	}
}


void displayGameState( struct gameState* G ) {
	int i;
      	int j;
 
       	printf("numPlayers:\t%d\n", G->numPlayers); //number of players
  
	for( i = 0 ; i < treasure_map+1 ; i++){
	   printf("supplyCount[%d]:\t%d\t",i,G->supplyCount[i]);
       	} //int supplyCount[treasure_map+1];  //this is the amount of a specific type of card given a specific number.

       	printf("\n");
 
	for( i = 0 ; i < treasure_map+1 ; i++){
	   printf("embargoTokens[%d]:\t%d\t",i,G->embargoTokens[i]);
      	}
  
	// int embargoTokens[treasure_map+1];
  
       	printf("\n");
 
       	printf("outpostPlayed:\t%d\n", G->outpostPlayed); 
       	// int outpostPlayed;
 
       	printf("outpostTurn:\t%d\n", G->outpostTurn);
  	// int outpostTurn;
  
	printf("whoseTurn:\t%d\n", G->whoseTurn);
  	// int whoseTurn;
 
       	printf("phase:\t%d\n", G->phase);
  	// int phase;

       	printf("numActions:\t%d\n", G->numActions);
  	// int numActions; /* Starts at 1 each turn */
   
	printf("coins:\t%d\n", G->coins);
 	// int coins; /* Use as you see fit! */
 
	printf("numBuys:\t%d\n", G->numBuys);
	// int numBuys; /* Starts at 1 each turn */
 
       	for ( i = 0 ; i < G->numPlayers ; i++ ){
    	   for ( j = 0 ; j < G->handCount[i] ; j++ ){
       	      printf("hand->[%d][%d] : %d\t", i , j , G->hand[i][j] );
    	   }
    	   printf("\n");
 	}
       
	// int hand[MAX_PLAYERS][MAX_HAND];
      	for ( i = 0 ; i <  G->numPlayers ; i++ ){
	   printf("handCount[%d]:\t%d\n", i , G->handCount[i]);
       	}

      	// int handCount[MAX_PLAYERS];
	for ( i = 0 ; i <  G->numPlayers ; i++ ){
	   for ( j = 0 ; j < G->deckCount[i] ; j++ ){
	      printf("deck[%d][%d] : %d\t", i , j , G->deck[i][j] );
	   }
	   printf("\n");
       	}  
 
       	// int deck[MAX_PLAYERS][MAX_DECK];
  	for ( i = 0 ; i <  G->numPlayers ; i++ ){
	   printf("deckCount[%d]:\t%d\n", i , G->deckCount[i]);
       	}
  
	// int deckCount[MAX_PLAYERS];
   	for ( i = 0 ; i <  G->numPlayers ; i++ ){
  	   for ( j = 0 ; j < G->discardCount[i] ; j++ ){
	      printf("discard[%d][%d] : %d\t", i , j , G->discard[i][j] );
	   }
	   printf("\n");
       	}  

      	// int discard[MAX_PLAYERS][MAX_DECK];
 	for ( i = 0 ; i <  G->numPlayers ; i++ ){
    	   printf("discardCount[%d]:\t%d\n", i , G->discardCount[i]);
	}

      	// int discardCount[MAX_PLAYERS];
	for ( i = 0 ; i < G->playedCardCount ; i++ ){
    	   printf("playedCards[%d]:\t%d\n", i , G->playedCards[i]);
	}

      	// int playedCards[MAX_DECK];
  	printf("playedCards:\t%d\n", G->playedCardCount);
	// int playedCardCount;
 
};

void makeRandomKingdomCards(int seed, int * cards){
	SelectStream(2);
	PutSeed((long)(seed));
	int currPos = 0;
	int nextNum;
	int c;
	nextNum = floor(Random()* 20 ) + 7;
	
	while (currPos < 10){
	   nextNum = floor(Random() * 20 ) + 7; 
	   printf("nextNum = %d\n", nextNum);
   	   for ( c = 0 ; c <= currPos ; c++ ){
	      if ( currPos <= c && cards[c] != nextNum ){
		 cards[currPos] = nextNum;
		 currPos++;
		 break;

	      } else if ( cards[c] == nextNum ){
		 break;
	      }
	   }
	}

	for ( c = 0 ; c < 10 ; c++){
	   printf("%d, ", cards[c]);
	}

	printf("\n");
	return ;
}

int playGame(int seed){

	printf("Running testdominion with seed : %d\n", seed);
 
	int players = seed%3 + 2;
     	int i;

      	printf("seed: %d;\tplayers: %d\n", seed, players);

       	struct gameState G;

      	int k[10]; 
      	makeRandomKingdomCards(seed, k );
  
      	for ( i = 0 ; i < 10 ; i++){
   	   printf("%d,", k[i]);
      	}

      	printf("\n");
      	printf ("Starting game.\n");
  
      	initializeGame(players, k, seed, &G);
  
       	int money = 0;
  	int smithyPos = -1;
  	int adventurerPos = -1;
  	i=0;
  	int numSmithies = 0;
  	int numAdventurers = 0;
  	int turnNum = 0;

 	while (!isGameOver(&G)) {
  	   turnNum++;
  	   printf("turn:\t%d\n", turnNum);
  	   displayGameState(&G);

	   if (turnNum > 500 ){
	      printf("error : turns over 500\n");
	      printf ("\nEND OF GAME seed : %d\n\n\n\n", seed);
	      return 1;
	   }

 	   money = 0;
 	   smithyPos = -1;
 	   adventurerPos = -1;
	   for (i = 0; i < numHandCards(&G); i++) {
	      if (handCard(i, &G) == copper)
	     	 money++;
	      else if (handCard(i, &G) == silver)
	     	 money += 2;
	      else if (handCard(i, &G) == gold)
	     	 money += 3;
	      else if (handCard(i, &G) == smithy)
	     	 smithyPos = i;
	      else if (handCard(i, &G) == adventurer)
	     	 adventurerPos = i;
       	   }

       	   if (whoseTurn(&G) == 0) {
	      if (smithyPos != -1) {
	 	 printf("0: smithy played from position %d\n", smithyPos); 
	 	 playCard(smithyPos, -1, -1, -1, &G); 
	 	 printf("smithy played.\n");
	 	 money = 0;
	 	 i=0;
	 	 while(i<numHandCards(&G)){
	  	    if (handCard(i, &G) == copper){
	   	       playCard(i, -1, -1, -1, &G);
	   	       money++;
	  	    }
	  	    else if (handCard(i, &G) == silver){
	   	       playCard(i, -1, -1, -1, &G);
	   	       money += 2;
	  	    }
	  	    else if (handCard(i, &G) == gold){
	   	       playCard(i, -1, -1, -1, &G);
	   	       money += 3;
	  	    }
	  	    i++;
	 	 }
 	      }

	      if (money >= 8) {
	 	 printf("0: bought province\n"); 
	 	 buyCard(province, &G);
	      }
	      else if (money >= 6) {
	 	 printf("0: bought gold\n"); 
	 	 buyCard(gold, &G);
	      }
	      else if ((money >= 4) && (numSmithies < 2)) {
	 	 printf("0: bought smithy\n"); 
	 	 buyCard(smithy, &G);
	 	 numSmithies++;
	      }
	      else if (money >= 3) {
	 	 printf("0: bought silver\n"); 
	 	 buyCard(silver, &G);
	      }

	      
	      printf("0: end turn\n");
     	      endTurn(&G);
    	   }
    
	   else {
      	      if (adventurerPos != -1) {
      	       	 printf("1: adventurer played from position %d\n", adventurerPos);
		 playCard(adventurerPos, -1, -1, -1, &G); 
	 	 money = 0;
	 	 i=0;

		 
		 while(i<numHandCards(&G)){
		    if (handCard(i, &G) == copper){
		       playCard(i, -1, -1, -1, &G);
	   	       money++;         
	  	    }
	  	    else if (handCard(i, &G) == silver){
	   	       playCard(i, -1, -1, -1, &G);
	   	       money += 2;
	  	    }
	  	    else if (handCard(i, &G) == gold){
	   	       playCard(i, -1, -1, -1, &G);
	   	       money += 3;
	  	    }
		    i++;
	 	 }
	      }

	      if (money >= 8) {
	 	 printf("1: bought province\n");
	 	 buyCard(province, &G);
	      }
	      else if ((money >= 6) && (numAdventurers < 2)) {
	 	 printf("1: bought adventurer\n");
	 	 buyCard(adventurer, &G);
	 	 numAdventurers++;
	      }else if (money >= 6){
	 	 printf("1: bought gold\n");
     		 buyCard(gold, &G);
      	      }
	      else if (money >= 3){
	 	 printf("1: bought silver\n");
     		 buyCard(silver, &G);
	      }
	      printf("1: endTurn\n");
      
	      endTurn(&G);      
       	   }
      	} // end of While

      	printf ("Finished game.\n");
      	for ( i= 0 ; i < players ; i++){
   	   printf ("Player [%d]: %d\n", i, scoreFor(i, &G) );
      	}

       	printf ("\nEND OF GAME seed : %d\n\n\n\n", seed);

      	return 0;
}
